致胜优势INFO
联系我们CONTACT

公司地址:茂名市人民南路新村大院22号101

电话:13592986386

pycharm编程日志2 语法您当前的位置:首页 > pycharm编程日志2 语法

pycharm编程日志2 语法

发布时间:2024/9/3 21:24:19

%s,表示格式化一个对象为字符


text = "Hello, %s!"
print(text % ("world",))  # 使用元组传递单个参数
# 或者

print(text % "world")  # 如果只有一个参数,可以直接传递字符串





【Python】流程图神器PygraphViz详解

展示表数据
pip install Jinja2
from jinja2 import Template
 
# 准备表格数据
table_data = [
    {"name": "Alice", "age": 30, "city": "New York"},
    {"name": "Bob", "age": 25, "city": "Paris"},
    {"name": "Charlie", "age": 35, "city": "London"}
]
 
# 准备Jinja2模板
template = Template("""
<!DOCTYPE html>
<html>
<head>
    <title>Table Data</title>
</head>
<body>
    <table border="1">
        <tr>
            <th>Name</th>
            <th>Age</th>
            <th>City</th>
        </tr>
        {% for row in table_data %}
        <tr>
            <td>{{ row.name }}</td>
            <td>{{ row.age }}</td>
            <td>{{ row.city }}</td>
        </tr>
        {% endfor %}
    </table>
</body>
</html>
""")


# 渲染HTML
html = template.render(table_data=table_data)
 
# 打印或写入文件
print(html)


格式化代码
pip install black
终端black your_script.py



# 使用dict()函数创建空字典  
d2 = dict()  
print(d2, type(d2))  # 输出: {} <class 'dict'>  
  
# 使用关键字参数创建字典  
d3 = dict(a=10, b=20, c=30)  
print(d3, type(d3))  # 输出: {'a': 10, 'b': 20, 'c': 30} <class 'dict'>  
  
# 使用包含键值对的列表(每个元素为元组)创建字典  
d4 = dict([("string1", "123"), ("string2", 456), ("a", 30), ("name", "malei")])  
print(d4, type(d4))  # 输出: {'string1': '123', 'string2': 456, 'a': 30, 'name': 'malei'} <class 'dict'>  
  
# 或者使用关键字参数形式,与直接赋值类似  
d4 = dict(string1="123", string2=456, a=30, name="malei")  
print(d4, type(d4))  # 输出同上



filter函数应用场景、filter函数可以用于从序列中筛选出符合特定条件的元素。例如,从一个列表中筛选出所有偶数:
filter函数可以用于从序列中筛选出符合特定条件的元素。例如,从一个列表中筛选出所有偶数:
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9]  
even_numbers = filter(lambda x: x % 2 == 0, numbers)  
print(list(even_numbers))  # Output: [2, 4, 6, 8]


sorted()函数返回一个与原始可迭代对象类型相同的新可迭代对象,其中所有元素都已按指定规则排序。
numbers = [5, 2, 4, 1, 3]
sorted_numbers = sorted(numbers)
print(sorted_numbers)  # 输出:[1, 2, 3, 4, 5]
print(numbers)         # 输出:[5, 2, 4, 1, 3]



scores = {'Alice': 85, 'Bob': 70, 'Charlie': 90, 'David': 75}
sorted_scores = dict(sorted(scores.items(), key=lambda item: item[1], reverse=True))
print(sorted_scores)  # 输出:{'Charlie': 90, 'Alice': 85, 'David': 75, 'Bob': 70}
print(scores)         # 输出:{'Alice': 85, 'Bob': 70, 'Charlie': 90, 'David': 75}



from apscheduler.schedulers.background import BackgroundScheduler
import time
# 创建后台调度器
scheduler = BackgroundScheduler()
# 定义任务函数
def job():
 print("定时任务执行:", time.strftime("%Y-%m-%d %H:%M:%S"))
 # 添加定时任务,每隔5秒执行一次
scheduler.add_job(job, 'interval', seconds=5)
# 启动调度器
scheduler.start()
# 主线程等待一段时间后结束
time.sleep(20)
# 关闭调度器
scheduler.shutdown()
print("主线程结束")





Python中的meta = {}通常用于创建一个空的字典对象,名为meta。‌这个字典对象可以用来存储键值对,以便后续的操作和处理。在不同的上下文中,meta字典可以有不同的用途。

python @property 装饰器使一个方法可以像属性一样被使用,而不需要在调用的时候带上() 。接下来我们会深入了解一下我们什么时候需要使用它,并且在什么场景下需要用到它以及如何很好的使用它。